﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AusProject.OrderDataControls
{
    public enum DataBoxType
    {
        Unspecified,
        TextDataBox,
        NumberDataBox,
        DateDataBox,
        DropListDataBox,
        CheckDataBox,
        TickDataBox,
        AutoCalcDataBox,
        TableDataBox
    }

    public class DataBoxInfo
    {
        private int id;

        public int Id
        {
            get { return id; }
        }

        private string typeOfData;

        internal string TypeOfData
        {
            get { return typeOfData; }
            set { typeOfData = value; }
        }

        private string title;

        public string Title
        {
            get { return title; }
            set { title = value; }
        }

        private string editable;

        public string Editable
        {
            get { return editable; }
            set { editable = value; }
        }

        private string borderMode;
        
        public string BorderMode
        {
            get { return borderMode; }
            set { borderMode = value; }
        }

        private string defaultValue;

        public string DefaultValue
        {
            get { return defaultValue; }
            set { defaultValue = value; }
        }

        private string value;

        public string Value
        {
            get { return this.value; }
            set { this.value = value; }
        }

        private int left;

        public int Left
        {
            get { return left; }
            set { left = value; }
        }

        private int top;

        public int Top
        {
            get { return top; }
            set { top = value; }
        }

        private int width;

        public int Width
        {
            get { return width; }
            set { width = value; }
        }

        private int height;

        public int Height
        {
            get { return height; }
            set { height = value; }
        }

        private object attachedData;

        public object AttachedData
        {
            get { return attachedData; }
            set { attachedData = value; }
        }

        public int SummaryFieldId { get; set; }

        public System.Drawing.Font FontUsed { get; set; }


        public DataBoxInfo(int id, DataBoxType typeOfData, string title, bool editable, DataControlBorderMode borderMode, string defaultValue, int left, int top, int width, int height, object attachedData, int summaryFieldId, string value, DataBoxFont fontUsed)
        {
            this.id = id;

            switch (typeOfData)
            {
                case DataBoxType.TextDataBox:
                    TypeOfData = "TEXT";
                    break;
                case DataBoxType.NumberDataBox:
                    TypeOfData = "NUMBER";
                    break;
                case DataBoxType.DateDataBox:
                    TypeOfData = "DATE";
                    break;
                case DataBoxType.DropListDataBox:
                    TypeOfData = "DROPLIST";
                    break;
                case DataBoxType.CheckDataBox:
                    TypeOfData = "CHECK";
                    break;
                case DataBoxType.TickDataBox:
                    TypeOfData = "TICK";
                    break;
                case DataBoxType.AutoCalcDataBox:
                    TypeOfData = "AUTOCALC";
                    break;
                case DataBoxType.TableDataBox:
                    TypeOfData = "TABLE";
                    break;
            }

            Title = title;

            if (editable)
            {
                Editable = "YES";
            }
            else
            {
                Editable = "NO";
            }

            switch (borderMode)
            {
                case DataControlBorderMode.None:
                    BorderMode = "NONE";
                    break;
                case DataControlBorderMode.Full:
                    BorderMode = "FULL";
                    break;
                case DataControlBorderMode.TitleOnly:
                    BorderMode = "TITLE";
                    break;
                case DataControlBorderMode.ValueOnly:
                    BorderMode = "VALUE";
                    break;
            }

            DefaultValue = defaultValue;

            Left = left;
            Top = top;
            Width = width;
            Height = height;

            AttachedData = attachedData;

            SummaryFieldId = summaryFieldId;

            FontUsed = new System.Drawing.Font(fontUsed.FontName, fontUsed.FontSize, 
                (fontUsed.FontBold ? System.Drawing.FontStyle.Bold : System.Drawing.FontStyle.Regular) 
                | (fontUsed.FontItalic ? System.Drawing.FontStyle.Italic : System.Drawing.FontStyle.Regular));

            Value = value;
        }


        public DataBox CreateDataBox()
        {
            DataBox dataBox = null;

            if (TypeOfData.ToUpper().CompareTo("TEXT") == 0)
            {
                dataBox = new TextDataBox();
            }
            else if (TypeOfData.ToUpper().CompareTo("NUMBER") == 0)
            {
                dataBox = new NumberDataBox();
            }
            else if (TypeOfData.ToUpper().CompareTo("DATE") == 0)
            {
                dataBox = new DateDataBox();
            }
            else if (TypeOfData.ToUpper().CompareTo("DROPLIST") == 0)
            {
                dataBox = new DropListDataBox();

                List<string> listValue = (List<string>)this.AttachedData;
                ((DropListDataBox)dataBox).ListValue = listValue;
            }
            else if (TypeOfData.ToUpper().CompareTo("CHECK") == 0)
            {
                dataBox = new CheckDataBox();
            }
            else if (TypeOfData.ToUpper().CompareTo("TICK") == 0)
            {
                dataBox = new TickDataBox();

                string[] optionLabels = (string[])this.AttachedData;
                if ((optionLabels != null) && (optionLabels.Length == 2))
                {
                    ((TickDataBox)dataBox).Option1Label = optionLabels[0];
                    ((TickDataBox)dataBox).Option2Label = optionLabels[1];
                }
                else
                {
                    ((TickDataBox)dataBox).Option1Label = "Yes";
                    ((TickDataBox)dataBox).Option1Label = "No";
                }
            }
            else if (TypeOfData.ToUpper().CompareTo("AUTOCALC") == 0)
            {
                //TODO:
                //dataBox = new NumberDataBox();
            }
            else if (TypeOfData.ToUpper().CompareTo("TABLE") == 0)
            {
                TableDataBox tableBox = new TableDataBox();
                List<List<DataBoxInfo>> rows = (List<List<DataBoxInfo>>)this.AttachedData;
                tableBox.TemplateDataBoxes = rows[0];
                foreach (DataBoxInfo subBox in rows[0])
                {
                    tableBox.AddNewControl(subBox.CreateDataBox());
                }
                for (int i = 1; i < rows.Count; i++)
                {
                    System.Windows.Forms.Panel nRow = tableBox.AddNewRow();
                    foreach (DataBoxInfo subBox in rows[i])
                    {
                        tableBox.AddNewControl(nRow, subBox.CreateDataBox());
                    }
                }

                dataBox = tableBox;
            }

            dataBox.Title = this.Title;

            dataBox.Editable = (this.Editable.ToUpper().CompareTo("YES") == 0);

            if (this.BorderMode.ToUpper().CompareTo("FULL") == 0)
            {
                dataBox.BorderMode = DataControlBorderMode.Full;
            }
            else if (this.BorderMode.ToUpper().CompareTo("TITLE") == 0)
            {
                dataBox.BorderMode = DataControlBorderMode.TitleOnly;
            }
            else if (this.BorderMode.ToUpper().CompareTo("VALUE") == 0)
            {
                dataBox.BorderMode = DataControlBorderMode.ValueOnly;
            }
            else
            {
                dataBox.BorderMode = DataControlBorderMode.None;
            }

            dataBox.Font = this.FontUsed;
            dataBox.Left = this.Left;
            dataBox.Top = this.Top;
            dataBox.Width = this.Width;
            dataBox.Height = this.Height;
            dataBox.DefaultValue = this.DefaultValue;
            dataBox.SummaryFieldId = this.SummaryFieldId;
            dataBox.Value = this.Value;

            return dataBox;
        }
    }


    public class DataBoxFont
    {
        public string FontName { get; set; }
        public float FontSize { get; set; }
        public bool FontBold { get; set; }
        public bool FontItalic { get; set; }

        public DataBoxFont(string name, float size, bool bold, bool italic)
        {
            FontName = name;
            FontSize = size;
            FontBold = bold;
            FontItalic = italic;
        }
    }
}
